<script>
$(document).ready(function(){
    $("#commentManager").ajaxForm({
        dataType:  'json',
        success: showResponse
    });

    function showResponse(res) {
        if (!res.success) {
            var message = '';
            $.each(res.error,function(i,v){
                $.each(v,function(ii,vv){
                      $("#commentManager #th-"+ii) .addClass('ui-state-error');
                        $.each(vv,function(iii,vvv){
                            message += $("#commentManager #th-"+ii).html()+': '+ vvv+ "\n";
                        });
                });
            });
           jAlert(message, '<?php echo $this->translate("checking errors")?>');
        }
        else{
            jAlert('',"<?php echo $this->translate('Successfully saved')?>");
            getCommentList();
        }
    }

    $(".deleteBtn").click(function(){
        if (confirm('<?php echo $this->translate("Are you sure want to delete ?")?>')) {
            var sn= $(this).attr('id').substr(4);
            var this_tr = $(this);
            $.getJSON('<?php echo $this->route("comment-delete")?>',{sn:sn},function(){
                this_tr.parents('tr').remove();
                checkCommentManagerIsEmpty();

             });
        }
      });

    $("#links a").click(function(){
       var url = $(this).attr('url');
        $.get('<?php echo $this->route("comment-list-table")?>'+url,
                {
                    kindSn: $("#selKind").val(),
                    levelSn: $("#selLevel").val()
                },
                function(data){
                    $("#comment-list-table").html(data);
                    //檢查編修狀態
                    checkCommentManagerIsEmpty();
                    }
                );
      });
     // 目前的頁數加入 currentPage class
    $("#links a[page='<?php echo $this->currentPage?>']").addClass('currentPage');
});
</script>
<style>
.currentPage {background:#ff0; font-weight: bold;}
</style>
<form id="commentManager" action="<?php echo $this->route('comment-save')?>" method="post">
<input type="hidden" name="kind_sn" value="<?php echo $this->kindSn?>" />
<input type="hidden" name="level_sn" value="<?php echo $this->levelSn?>" />
<table class="hor-minimalist-a" summary="score comment levellist" style="margin:5px;width:300px">
<thead>
<tr>
<th scope="col" id="th-text"><?php echo $this->translate('comment')?></th>
<th scope="col" ><?php echo $this->translate('action')?></th>
</tr></thead>
<tbody>
<?php foreach($this->data as $val):?>
<tr>
<td><input type="text"  id="name-<?php echo $val['sn']?>" name="name[<?php echo $val['sn']?>]" value="<?php echo $val['text']?>"  size="20"/></td>
<td><input type="button" class="deleteBtn"  id="del-<?php echo $val['sn']?>" value="<?php echo $this->translate('delete')?>" /></td>
</tr>
<?php endforeach;?>
</tbody>
</table>
</form>
<div id="links">
    Pages: <?php echo  $this->layout->display(null, true); ?>
   </div>
